/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sesame.domain.Message;
import com.sesame.service.MessageService;
import com.sesame.service.PageService;
import com.sesame.toolkit.Page;

/**
 * @author shubeta
 * 
 */
public class OutboxController extends AbstractController {

    private MessageService messageService;
    private PageService pageService;

    public void setPageService(PageService pageService) {
        this.pageService = pageService;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        String[] messages = request.getParameterValues("outbox");
        if (messages != null) {
            messageService.deleteMessage(messages, "in");
        }

        // 分页相关
        String currentPage = request.getParameter("page");
        Integer senderId = (Integer) request.getSession()
                .getAttribute("userId");
        Page page = pageService.paging(request, currentPage);
        page.setRecordCount(messageService.getSendedMessageCount(senderId));
        page.setId(senderId);

        Map model = new HashMap();
        String nickName = (String) request.getSession()
                .getAttribute("nickName");
        List<Message> messageList = messageService.listMessageBySender(page);
        model.put("outbox", messageList);
        model.put("page", page);

        return new ModelAndView("outbox", model);

    }

}